package com.tdtxdcxm.mynotepad.provider;

import com.tdtxdcxm.mynotepad.ResourceTable;
import com.tdtxdcxm.mynotepad.listcontaineritem.NoteListItem;
import com.tdtxdcxm.mynotepad.notedata.NoteDataManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;


import java.util.List;

public class NoteListItemProvider extends BaseItemProvider {
    private float x1 = 0,x2 = 0,y1 = 0,y2 = 0;
    private List<NoteListItem> list;
    private AbilitySlice slice;
    public static NoteListItemProvider noteListItemProvider = null;
    public static DirectionalLayout main_rootdl_ddl1 = null;

    public NoteListItemProvider(List<NoteListItem> list,AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list != null?list.size():0;
    }

    @Override
    public Object getItem(int i) {
        if(list == null || (i < 0 || i >= list.size())){
            return null;
        }
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cmpt;
        if(component == null){
            cmpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_main_listcontainer_itemlayout,null,false);
        }
        else{
            cmpt = component;
        }

        NoteListItem noteListItem = list.get(i);
        Text selecttxt = (Text) cmpt.findComponentById(ResourceTable.Id_item_selecttxt);
        selecttxt.setText("N");
        selecttxt.setTextColor(Color.WHITE);
        selecttxt.setVisibility(Component.HIDE);
        Text text = (Text) cmpt.findComponentById(ResourceTable.Id_item_text);
        text.setText(noteListItem.getShortText());

        NoteListItemProvider.noteListItemProvider = this;

        Button button = (Button) cmpt.findComponentById(ResourceTable.Id_item_deletebut);
        button.setVisibility(Component.HIDE);

        selecttxt.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("selecttxt is["+selecttxt.getText()+"]");

                if(selecttxt.getText().equals("N")){
                    selecttxt.setText("✔");
                    selecttxt.setTextColor(Color.GREEN);
                    return;
                }
                if(selecttxt.getText().equals("✔")){
                    selecttxt.setText("N");
                    selecttxt.setTextColor(Color.WHITE);
                    return;
                }
            }
        });

        text.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                int action = touchEvent.getAction();
                if(action == TouchEvent.PRIMARY_POINT_DOWN){
                    MmiPoint mmiPoint = touchEvent.getPointerPosition(0);
                    x1 = mmiPoint.getX();
                    y1 = mmiPoint.getY();
                }
                if(action == TouchEvent.PRIMARY_POINT_UP){
                    MmiPoint mmiPoint = touchEvent.getPointerPosition(0);
                    x2 = mmiPoint.getX();
                    y2 = mmiPoint.getY();

                    if(x1 < x2 && Math.abs(y2 - y1) <= 90.0){
                        button.setVisibility(Component.HIDE);
                    }
                    if(x1 > x2 && Math.abs(y2 - y1) <= 90.0){
                        button.setVisibility(Component.VISIBLE);
                    }
                    if(y1 > y2 && Math.abs(x2 - x1) <= 90.0){
                        main_rootdl_ddl1.setVisibility(Component.HIDE);
                    }
                    if(y1 < y2 && Math.abs(x2 - x1) <= 90.0){
                        main_rootdl_ddl1.setVisibility(Component.VISIBLE);
                    }
                }
                return true;
            }
        });
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                System.out.println("list.get(i).getFile() is"+list.get(i).getFile());
                NoteDataManager.deleteFolderFiles(list.get(i).getFile());
                list.remove(i);
                NoteListItemProvider.this.notifyDataChanged();
            }
        });
        return cmpt;
    }
}
